import { IRoutes } from '@todo/core';
import { authGuard } from '../../auth/authGuard.ts';
import SwitchAccountRoundedIcon from '@mui/icons-material/SwitchAccountRounded';
import { lazy } from 'react';
import { GetRolesListUseCase } from '../usecase/getRolesListUseCase.ts';
import { GetPermissionListUseCase } from '../usecase/getPermissionListUseCase.ts';
import { DisposeRoleListUseCase } from '../usecase/disposeRoleListUseCase.ts';

export const USER_ROUTES = {
  USER: 'user',
  LIST: 'list',
  ROLES: 'roles',
};

export const routes: IRoutes = [
  {
    name: USER_ROUTES.USER,
    path: '/user',
    title: "Let's interview!",
    auth: authGuard,
    menu: {
      text: 'user:menu.user',
      sortOrder: 100,
      icon: SwitchAccountRoundedIcon,
    },
    children: [
      {
        name: USER_ROUTES.LIST,
        path: '/list',
        title: "Let's interview!",
        auth: authGuard,
        menu: {
          text: 'user:menu.user.list',
          sortOrder: 100,
          navigate: {
            path: 'user.list',
          },
        },
      },
      {
        name: USER_ROUTES.ROLES,
        path: '/roles/',
        title: "Let's interview!",
        auth: authGuard,
        pageComponent: lazy(
          () => import('../view/UserRolesPage/UserRolesPage.tsx'),
        ),
        menu: {
          text: 'user:menu.user.role',
          sortOrder: 100,
          navigate: {
            path: 'user.roles',
          },
        },
        onEnter: async (_router, container) => {
          const getPermissionListUseCase =
            container.get<GetPermissionListUseCase>(GetPermissionListUseCase);
          const getRolesListUseCase =
            container.get<GetRolesListUseCase>(GetRolesListUseCase);

          await getPermissionListUseCase.execute();
          await getRolesListUseCase.execute({
            perPage: '20',
            page: '1',
            orderBy: 'name',
            orderDirection: 'asc',
          });
        },
        onExit: (_router, _container) => {
          _container
            .get<DisposeRoleListUseCase>(DisposeRoleListUseCase)
            .execute();
        },
      },
    ],
  },
];
